// These are meant to be typed into the REPL. You can also run
// scala -Xnojline < repl-session.scala to run them all at once.

/**
 * List是可变的
 * https://www.runoob.com/scala/scala-lists.html
 */

val digits = List(4, 2)

// 右结合操作符
9 :: List(4, 2)

// Nil表示空列表
9 :: 4 :: 2 :: Nil

9 :: (4 :: (2 :: Nil))

/**
 * 递归计算list元素的和
 * @param lst
 * @return
 */
def sum(lst: List[Int]): Int = if (lst == Nil) 0 else lst.head + sum(lst.tail)

sum(List(9, 4, 2)) // res83: Int = 15

/**
 * 遍历
 */
for( i <- List(9, 4, 2)) println(i)

/**
 * 使用模式匹配进行递归求和
 * @param lst
 * @return
 */
def sum(lst: List[Int]): Int = lst match {
  case Nil => 0
  case h :: t => h + sum(t) // h is lst.head, t is lst.tail
}

sum(List(9, 4, 2))

List(9, 4, 2).sum
